using System;
using System.Configuration;
using System.Collections.Generic;

namespace uk.co.tonyditchfield.tort.database.providers
{
    internal class ServerDetails
    {
        internal ServerDetails()
        {
            BuildReservedWords();
        }

        private database.engine.DBEngine engine;
        internal database.engine.DBEngine Engine
        {
            get 
            {
                switch (DBName.ToUpper())
                {
                    case "SQLSERVER2005":
                        engine = database.engine.DBEngine.SqlServer2005;
                        break;
                    case "SQLSERVER2000":
                        engine = database.engine.DBEngine.SqlServer2000;
                        break;
                    case "ORACLE9":
                        engine = database.engine.DBEngine.Oracle9i;
                        break;
                    case "ORACLE10":
                        engine = database.engine.DBEngine.Oracle10g;
                        break;
                }
                return engine;
            }
        }

        private static string serverVersion = null;
        internal static string ServerVersion
        {
            get { return ServerDetails.serverVersion; }
            set { ServerDetails.serverVersion = value; }
        }

        internal string DBName
        {
            get { return ConfigurationManager.AppSettings.Get("DBEngine"); }
        }

        internal string Connection
        {
            get { return ConfigurationManager.AppSettings.Get("DBConnection"); }
        }

        internal bool IgnoreUnicode
        {
            get { return ConfigurationManager.AppSettings.Get("IgnoreUnicode") == "True" ? true : false; }
        }

        private void BuildReservedWords()
        {
            switch (Engine)
            {
                case database.engine.DBEngine.SqlServer2005:
                    ReservedWords = (new uk.co.tonyditchfield.utility.ResourceListBuilder().
                        ConvertResourceToCollection(Properties.Resources.SQL2005ReservedWords));
                    break;
                case database.engine.DBEngine.SqlServer2000:
                    ReservedWords = (new uk.co.tonyditchfield.utility.ResourceListBuilder().
                        ConvertResourceToCollection(Properties.Resources.SQL2000ReservedWords));
                    break;
                case database.engine.DBEngine.Oracle9i:
                    ReservedWords = (new uk.co.tonyditchfield.utility.ResourceListBuilder().
                        ConvertResourceToCollection(Properties.Resources.Ora9iReservedWords));
                    break;
                case database.engine.DBEngine.Oracle10g:
                    ReservedWords = (new uk.co.tonyditchfield.utility.ResourceListBuilder().
                        ConvertResourceToCollection(Properties.Resources.Ora10gReservedWords));
                    break;
            }
        }

        private ICollection<string> reservedWords = new List<string>();
        internal ICollection<string> ReservedWords
        {
            get { return reservedWords; }
            set { reservedWords = value; }
        }
    }
}
